package com.hy.Result;

import com.hy.AppHttpCodeEnum;

import java.io.Serializable;

public class ResponseResult<T> implements Serializable {

    private String host;

    private Integer code;

    private String errorMessage;

    private T data;

    public ResponseResult() {
        this.code = 200;
    }

    public ResponseResult(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.errorMessage = msg;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg) {
        this.code = code;
        this.errorMessage = msg;
    }

    public static ResponseResult errorResult(int code, String msg) {
        ResponseResult result = new ResponseResult();
        return result.error(code, msg);
    }

    public static ResponseResult okResult(int code, String msg) {
        ResponseResult result = new ResponseResult();
        return result.ok(code, null, msg);
    }

    public static ResponseResult okResult(int code, String msg,Object data) {
        ResponseResult result = new ResponseResult();
        return result.ok(code, data,msg);
    }

    public static ResponseResult okResult(Object data) {
        ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public static ResponseResult errorResult(AppHttpCodeEnum enums) {
        return setAppHttpCodeEnum(enums, enums.getErrorMessage());
    }

    public static ResponseResult errorResult(AppHttpCodeEnum enums, String errorMessage) {
        return setAppHttpCodeEnum(enums, errorMessage);
    }

    public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums) {
        return okResult(enums.getCode(), enums.getErrorMessage());
    }

    private static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums, String errorMessage) {
        return okResult(enums.getCode(), errorMessage);
    }

    public static ResponseResult okResult() {
        return okResult(AppHttpCodeEnum.SUCCESS.getCode(), AppHttpCodeEnum.SUCCESS.getErrorMessage());
    }

    public ResponseResult<?> error(Integer code, String msg) {
        this.code = code;
        this.errorMessage = msg;
        return this;
    }

    public ResponseResult<?> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public ResponseResult<?> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.errorMessage = msg;
        return this;
    }

    public ResponseResult<?> ok(T data) {
        this.data = data;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }


    public static void main(String[] args) {
        //前置
        /*AppHttpCodeEnum success = AppHttpCodeEnum.SUCCESS;
        System.out.println(success.getCode());
        System.out.println(success.getErrorMessage());*/

        //查询一个对象
        /*Map map = new HashMap();
        map.put("name","zhangsan");
        map.put("age",18);
        ResponseResult result = ResponseResult.okResult(map);
        System.out.println(JSON.toJSONString(result));*/


        //新增，修改，删除  在项目中统一返回成功即可
       /* ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
        System.out.println(JSON.toJSONString(result));*/


        //根据不用的业务返回不同的提示信息  比如：当前操作需要登录、参数错误
        /*ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        System.out.println(JSON.toJSONString(result));*/

        //查询分页信息
//        PageResponseResult responseResult = new PageResponseResult(1,5,50);
//        List list = new ArrayList();
//        list.add("itcast");
//        list.add("it");
//        responseResult.setData(list);
//        System.out.println(JSON.toJSONString(responseResult));




//        点击立即购买,跳转到确认订单页面,完成对应的后端代码,业务逻辑,如(mapper层,对应的xml,service层,Controller层) 加注释,CREATE TABLE `user_address` (
//  `address_id` bigint NOT NULL AUTO_INCREMENT COMMENT '地址ID，主键',
//  `user_id` bigint NOT NULL COMMENT '用户ID，关联用户表',
//  `name` varchar(255) NOT NULL COMMENT '收货人姓名',
//  `phone` varchar(20) NOT NULL COMMENT '收货人电话',
//  `province_id` int NOT NULL COMMENT '省份ID，外键',
//  `city_id` int NOT NULL COMMENT '城市ID，外键',
//  `district_id` int NOT NULL COMMENT '区/县ID，外键',
//  `detail` varchar(255) NOT NULL COMMENT '详细地址',
//  `is_default` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否为默认地址: 0-否, 1-是',
//  `tag` varchar(50) DEFAULT NULL COMMENT '标签: 如 "家"、"公司" 等',
//  PRIMARY KEY (`address_id`),
//  KEY `province_id` (`province_id`),
//  KEY `city_id` (`city_id`),
//  KEY `district_id` (`district_id`),
//  CONSTRAINT `user_address_ibfk_1` FOREIGN KEY (`province_id`) REFERENCES `province` (`id`) ON DELETE CASCADE,
//  CONSTRAINT `user_address_ibfk_2` FOREIGN KEY (`city_id`) REFERENCES `city` (`id`) ON DELETE CASCADE,
//  CONSTRAINT `user_address_ibfk_3` FOREIGN KEY (`district_id`) REFERENCES `district` (`id`) ON DELETE CASCADE
//) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用户地址表';
//CREATE TABLE `order_item` (
//  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '订单项ID',
//  `order_id` varchar(255) NOT NULL COMMENT '订单ID',
//  `medicine_id` bigint NOT NULL COMMENT '药品ID',
//  `medicine_name` varchar(255) NOT NULL COMMENT '药品名称',
//  `image_url` varchar(255) DEFAULT NULL COMMENT '药品图片',
//  `specification` varchar(255) DEFAULT NULL COMMENT '药品规格',
//  `dosage_form` varchar(255) DEFAULT NULL COMMENT '药品剂型',
//  `price` decimal(10,2) NOT NULL COMMENT '单价',
//  `quantity` int NOT NULL COMMENT '数量',
//  `total_price` decimal(10,2) NOT NULL COMMENT '总价',
//  PRIMARY KEY (`id`)
//) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='订单项目表';
//CREATE TABLE `medicine_order` (
//  `order_id` int NOT NULL AUTO_INCREMENT COMMENT '订单唯一标识',
//  `user_id` int NOT NULL COMMENT '下单用户ID',
//  `medicine_id` int NOT NULL COMMENT '关联 medicine_basic_info 的 medicine_id',
//  `order_quantity` int NOT NULL DEFAULT '1' COMMENT '购买数量',
//  `order_amount` decimal(10,2) DEFAULT NULL COMMENT '订单金额',
//  `order_status` tinyint NOT NULL DEFAULT '0' COMMENT '订单状态：0-待支付，1-已支付，2-已发货，3-已完成，4-已取消',
//  `payment_time` datetime DEFAULT NULL COMMENT '支付时间',
//  `delivery_time` datetime DEFAULT NULL COMMENT '发货时间',
//  `finish_time` datetime DEFAULT NULL COMMENT '完成时间',
//  `cancel_time` datetime DEFAULT NULL COMMENT '取消时间',
//  PRIMARY KEY (`order_id`) USING BTREE,
//  KEY `medicine_id` (`medicine_id`) USING BTREE,
//  CONSTRAINT `medicine_order_ibfk_1` FOREIGN KEY (`medicine_id`) REFERENCES `medicine_basic_info` (`medicine_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
//) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT='药品订单表';
//CREATE TABLE `medicine_basic_info` (
//  `medicine_id` int NOT NULL AUTO_INCREMENT COMMENT '药品唯一标识',
//  `medicine_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '药品名称',
//  `image_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '药品图片',
//  `specification` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '药品规格',
//  `dosage_form` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '剂型',
//  `approval_number` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '药品批准文号',
//  `manufacturer` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '生产厂家',
//  `shelf_life` int DEFAULT NULL COMMENT '保质期（月）',
//  `storage_condition` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '储存条件',
//  `ingredients` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '药品成分',
//  `indications` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '适应症',
//  `contraindications` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '禁忌症',
//  `common_category_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '关联 med_common_category 的 id',
//  `prescription_category_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '关联 prescription_category 的 id',
//  `sales_volume` int NOT NULL DEFAULT '0' COMMENT '总销量',
//  `price` decimal(10,2) NOT NULL COMMENT '药品单价',
//  `deleted` int NOT NULL DEFAULT '1' COMMENT '0: 删除 1:存在',
//  `usage_method` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '用法用量',
//  `precaution_notes` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '注意事项',
//  `side_effect_descriptions` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '不良反应',
//  `drug_interaction_details` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '药物相互作用',
//  `minStock` int NOT NULL DEFAULT '0' COMMENT '库存',
//  PRIMARY KEY (`medicine_id`) USING BTREE,
//  UNIQUE KEY `uk_approval_number` (`approval_number`) USING BTREE,
//  KEY `common_category_id` (`common_category_id`) USING BTREE,
//  KEY `prescription_category_id` (`prescription_category_id`) USING BTREE,
//  CONSTRAINT `medicine_basic_info_ibfk_1` FOREIGN KEY (`common_category_id`) REFERENCES `med_common_category` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
//  CONSTRAINT `medicine_basic_info_ibfk_2` FOREIGN KEY (`prescription_category_id`) REFERENCES `prescription_category` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
//) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT='药品基本信息表';
//CREATE TABLE `ap_phoneuser` (
//  `id` bigint NOT NULL AUTO_INCREMENT,
//  `phone` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
//  `password_hash` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
//  `username` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `gender` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT 'U',
//  `email` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `status` tinyint(1) DEFAULT '1',
//  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
//  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
//  `avatar_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
//  PRIMARY KEY (`id`) USING BTREE,
//  UNIQUE KEY `phone` (`phone`) USING BTREE
//) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC;用户表
//CREATE TABLE `medicine_order` (
//  `order_id` int NOT NULL AUTO_INCREMENT COMMENT '订单唯一标识',
//  `user_id` int NOT NULL COMMENT '下单用户ID',
//  `medicine_id` int NOT NULL COMMENT '关联 medicine_basic_info 的 medicine_id',
//  `order_quantity` int NOT NULL DEFAULT '1' COMMENT '购买数量',
//  `order_amount` decimal(10,2) DEFAULT NULL COMMENT '订单金额',
//  `order_status` tinyint NOT NULL DEFAULT '0' COMMENT '订单状态：0-待支付，1-已支付，2-已发货，3-已完成，4-已取消',
//  `payment_time` datetime DEFAULT NULL COMMENT '支付时间',
//  `delivery_time` datetime DEFAULT NULL COMMENT '发货时间',
//  `finish_time` datetime DEFAULT NULL COMMENT '完成时间',
//  `cancel_time` datetime DEFAULT NULL COMMENT '取消时间',
//  PRIMARY KEY (`order_id`) USING BTREE,
//  KEY `medicine_id` (`medicine_id`) USING BTREE,
//  CONSTRAINT `medicine_order_ibfk_1` FOREIGN KEY (`medicine_id`) REFERENCES `medicine_basic_info` (`medicine_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
//) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT='药品订单表'; 使用ResponseResult作为返回值,package com.hy.Result;
//
//import com.hy.AppHttpCodeEnum;
//
//import java.io.Serializable;
//
//public class ResponseResult<T> implements Serializable {
//
//    private String host;
//
//    private Integer code;
//
//    private String errorMessage;
//
//    private T data;
//
//    public ResponseResult() {
//        this.code = 200;
//    }
//
//    public ResponseResult(Integer code, T data) {
//        this.code = code;
//        this.data = data;
//    }
//
//    public ResponseResult(Integer code, String msg, T data) {
//        this.code = code;
//        this.errorMessage = msg;
//        this.data = data;
//    }
//
//    public ResponseResult(Integer code, String msg) {
//        this.code = code;
//        this.errorMessage = msg;
//    }
//
//    public static ResponseResult errorResult(int code, String msg) {
//        ResponseResult result = new ResponseResult();
//        return result.error(code, msg);
//    }
//
//    public static ResponseResult okResult(int code, String msg) {
//        ResponseResult result = new ResponseResult();
//        return result.ok(code, null, msg);
//    }
//
//    public static ResponseResult okResult(int code, String msg,Object data) {
//        ResponseResult result = new ResponseResult();
//        return result.ok(code, data,msg);
//    }
//
//    public static ResponseResult okResult(Object data) {
//        ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getErrorMessage());
//        if (data != null) {
//            result.setData(data);
//        }
//        return result;
//    }
//
//    public static ResponseResult errorResult(AppHttpCodeEnum enums) {
//        return setAppHttpCodeEnum(enums, enums.getErrorMessage());
//    }
//
//    public static ResponseResult errorResult(AppHttpCodeEnum enums, String errorMessage) {
//        return setAppHttpCodeEnum(enums, errorMessage);
//    }
//
//    public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums) {
//        return okResult(enums.getCode(), enums.getErrorMessage());
//    }
//
//    private static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums, String errorMessage) {
//        return okResult(enums.getCode(), errorMessage);
//    }
//
//    public static ResponseResult okResult() {
//        return okResult(AppHttpCodeEnum.SUCCESS.getCode(), AppHttpCodeEnum.SUCCESS.getErrorMessage());
//    }
//
//    public ResponseResult<?> error(Integer code, String msg) {
//        this.code = code;
//        this.errorMessage = msg;
//        return this;
//    }
//
//    public ResponseResult<?> ok(Integer code, T data) {
//        this.code = code;
//        this.data = data;
//        return this;
//    }
//
//    public ResponseResult<?> ok(Integer code, T data, String msg) {
//        this.code = code;
//        this.data = data;
//        this.errorMessage = msg;
//        return this;
//    }
//
//    public ResponseResult<?> ok(T data) {
//        this.data = data;
//        return this;
//    }
//
//    public Integer getCode() {
//        return code;
//    }
//
//    public void setCode(Integer code) {
//        this.code = code;
//    }
//
//    public String getErrorMessage() {
//        return errorMessage;
//    }
//
//    public void setErrorMessage(String errorMessage) {
//        this.errorMessage = errorMessage;
//    }
//
//    public T getData() {
//        return data;
//    }
//
//    public void setData(T data) {
//        this.data = data;
//    }
//
//    public String getHost() {
//        return host;
//    }
//
//    public void setHost(String host) {
//        this.host = host;
//    }
//
//
//    public static void main(String[] args) {
//        //前置
//        /*AppHttpCodeEnum success = AppHttpCodeEnum.SUCCESS;
//        System.out.println(success.getCode());
//        System.out.println(success.getErrorMessage());*/
//
//        //查询一个对象
//        /*Map map = new HashMap();
//        map.put("name","zhangsan");
//        map.put("age",18);
//        ResponseResult result = ResponseResult.okResult(map);
//        System.out.println(JSON.toJSONString(result));*/
//
//
//        //新增，修改，删除  在项目中统一返回成功即可
//       /* ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
//        System.out.println(JSON.toJSONString(result));*/
//
//
//        //根据不用的业务返回不同的提示信息  比如：当前操作需要登录、参数错误
//        /*ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
//        System.out.println(JSON.toJSONString(result));*/
//
//        //查询分页信息
////        PageResponseResult responseResult = new PageResponseResult(1,5,50);
////        List list = new ArrayList();
////        list.add("itcast");
////        list.add("it");
////        responseResult.setData(list);
////        System.out.println(JSON.toJSONString(responseResult));
//
//    }
//}
//不加泛型
    }
}
